<?php

declare(strict_types=1);

/**
 * The main Application class
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at https://mozilla.org/MPL/2.0/.
 *
 * @package   phpMyFAQ
 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
 * @copyright 2023-2025 phpMyFAQ Team
 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      https://www.phpmyfaq.de
 * @since     2023-10-24
 */

namespace phpMyFAQ;

use phpMyFAQ\Core\Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Throwable;

class Application
{
    private UrlMatcher $urlMatcher;

    private ControllerResolver $controllerResolver;

    public function __construct(
        private readonly ?ContainerInterface $container = null,
    ) {
    }

    /**
     * @throws Exception
     */
    public function run(RouteCollection $routeCollection): void
    {
        $currentLanguage = $this->setLanguage();
        $this->initializeTranslation($currentLanguage);
        Strings::init($currentLanguage);
        $request = Request::createFromGlobals();
        $requestContext = new RequestContext();
        $requestContext->fromRequest($request);
        $this->handleRequest($routeCollection, $request, $requestContext);
    }

    public function setUrlMatcher(UrlMatcher $urlMatcher): void
    {
        $this->urlMatcher = $urlMatcher;
    }

    public function setControllerResolver(ControllerResolver $controllerResolver): void
    {
        $this->controllerResolver = $controllerResolver;
    }

    private function setLanguage(): string
    {
        if (!is_null($this->container)) {
            $configuration = $this->container->get(id: 'phpmyfaq.configuration');
            $language = $this->container->get(id: 'phpmyfaq.language');
            $currentLanguage = $language->setLanguage(
                (bool) $configuration->get(item: 'main.languageDetection'),
                $configuration->get(item: 'main.language'),
            );

            require PMF_TRANSLATION_DIR . '/language_en.php';
            if (Language::isASupportedLanguage($currentLanguage)) {
                require PMF_TRANSLATION_DIR . '/language_' . strtolower($currentLanguage) . '.php';
            }

            $configuration->setLanguage($language);

            return $currentLanguage;
        }

        return 'en';
    }

    /**
     * @throws Exception
     */
    private function initializeTranslation(string $currentLanguage): void
    {
        try {
            Translation::create()
                ->setLanguagesDir(PMF_TRANSLATION_DIR)
                ->setDefaultLanguage(defaultLanguage: 'en')
                ->setCurrentLanguage($currentLanguage)
                ->setMultiByteLanguage();
        } catch (Exception $exception) {
            throw new Exception($exception->getMessage());
        }
    }

    private function handleRequest(
        RouteCollection $routeCollection,
        Request $request,
        RequestContext $requestContext,
    ): void {
        $urlMatcher = new UrlMatcher($routeCollection, $requestContext);
        $this->setUrlMatcher($urlMatcher);
        $controllerResolver = new ControllerResolver();
        $this->setControllerResolver($controllerResolver);
        $argumentResolver = new ArgumentResolver();
        $response = new Response();

        try {
            $this->urlMatcher->setContext($requestContext);
            $request->attributes->add($this->urlMatcher->match($request->getPathInfo()));
            $controller = $this->controllerResolver->getController($request);
            $arguments = $argumentResolver->getArguments($request, $controller);
            $response->setStatusCode(Response::HTTP_OK);
            $response = call_user_func_array($controller, $arguments);
        } catch (ResourceNotFoundException $exception) {
            $message = $this->formatExceptionMessage(
                template: 'Not Found: :message at line :line at :file',
                e: $exception,
            );
            $response = new Response(
                content: $message,
                status: Response::HTTP_NOT_FOUND,
            );
        } catch (UnauthorizedHttpException) {
            $response = new RedirectResponse(url: '/login');
            if (str_contains(
                haystack: $urlMatcher->getContext()->getBaseUrl(),
                needle: '/api',
            )) {
                $response = new Response(
                    content: json_encode(value: ['error' => 'Unauthorized access']),
                    status: Response::HTTP_UNAUTHORIZED,
                    headers: ['Content-Type' => 'application/json'],
                );
            }
        } catch (BadRequestException $exception) {
            $message = $this->formatExceptionMessage(
                template: 'An error occurred: :message at line :line at :file',
                e: $exception,
            );
            $response = new Response(
                content: $message,
                status: Response::HTTP_BAD_REQUEST,
            );
        }

        $response->send();
    }

    /**
     * Formats an exception message from a template with named placeholders.
     */
    private function formatExceptionMessage(string $template, Throwable $e): string
    {
        return strtr(
            string: $template,
            from: [
                ':message' => $e->getMessage(),
                ':line' => (string) $e->getLine(),
                ':file' => $e->getFile(),
            ],
        );
    }
}
